package chatServer;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.JList;
import messenger.Message;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Dimension;

public class LogWindow extends JDialog {
	/**
	 * 
	 */
	private static final long serialVersionUID = -4151231393034755656L;
	private ConversDB db;
	private final JPanel contentPanel = new JPanel();
	DefaultListModel<String> mlist = new DefaultListModel<String>();
	DefaultListModel<String> ulist = new DefaultListModel<String>();
	
	public void listMessages() {
		for(UserDB u : db.getUsers())
			if(db.getMessagelog().containsKey(u))
				if(db.getMessagelog().get(u).getMessages()!=null)
					for(Message l : db.getMessagelog().get(u).getMessages())
						if(l!=null)
							mlist.addElement(l.getDate().getTime().toString()+" "+l.getFrom()+"  -> "+l.getTo()+" ("+l.getBody()+")");
			
	}

	
	public void listUsers() {
		for(UserDB u : db.getUsers())
			if(u!=null)
				ulist.addElement(u.getUserName());
	}

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			//LogWindow dialog = new LogWindow();
			//dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			//dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 * @param db 
	 * @param i 
	 */
	public LogWindow(ConversDB db, int i) {
		setTitle("MessageLog");
		setBounds(100, 100, 450, 471);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		this.db=db;
	
		JList<String> list = new JList<String>(mlist);
		list.setPreferredSize(new Dimension(400, 300));
		list.setSize(new Dimension(400, 400));
		list.setBounds(1, 1, 423, 228);

		JScrollPane scrollPane = new JScrollPane(list);
		scrollPane.setBounds(0, 0, 424, 389);
		
		
		JList<String> list2 = new JList<String>(ulist);
		list2.setPreferredSize(new Dimension(400, 300));
		list2.setSize(new Dimension(400, 400));
		list2.setBounds(1, 1, 423, 228);

		JScrollPane scrollPane2 = new JScrollPane(list2);
		scrollPane2.setBounds(0, 0, 424, 389);
		if(i==0){
			listMessages();
			contentPanel.add(scrollPane);
		}
		else{
			listUsers();
			contentPanel.add(scrollPane2);}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						dispose();
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
		}
	}
}
